//
//  GKDYVideoControlView.m
//  GKDYVideo
//
//  Created by QuintGao on 2018/9/23.
//  Copyright © 2018 QuintGao. All rights reserved.
//

#import "LYouVideoControlView.h"
#import "LYSliderView.h"

@interface GKDYVideoItemButton : UIButton

@end

@implementation GKDYVideoItemButton

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.imageView sizeToFit];
    [self.titleLabel sizeToFit];
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    CGFloat imgW = self.imageView.frame.size.width;
    CGFloat imgH = self.imageView.frame.size.height;
    
    self.imageView.frame = CGRectMake((width - imgH) / 2, 0, imgW, imgH);
    
    CGFloat titleW = self.titleLabel.frame.size.width;
    CGFloat titleH = self.titleLabel.frame.size.height;
    
    self.titleLabel.frame = CGRectMake((width - titleW) / 2, height - titleH, titleW, titleH);
}

@end

@interface LYouVideoControlView()

@property (nonatomic, strong) UIImageView           *iconView;
@property (nonatomic, strong) UIActivityIndicatorView   *loadingView;

@end

@implementation LYouVideoControlView

- (instancetype)init {
    if (self = [super init]) {
        [self addSubview:self.coverImgView];
        [self addSubview:self.favoriteBtn];
        [self addSubview:self.detailBtn];
        [self addSubview:self.loadingView];
        [self addSubview:self.playBtn];
        

        [self.coverImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [self.favoriteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-25);
            make.bottom.equalTo(self).offset(-18);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(30);
        }];
        
        [self.detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self).offset(-15);
            make.height.mas_equalTo(35);
            make.width.mas_equalTo(80);
        }];
        
        [self.loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
        
        [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(controlViewDidClick:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)setModel:(LYouVideoModel *)model {
    _model = model;
    [self.coverImgView sd_setImageWithURL:[NSURL URLWithString:model.thumbnail_url] placeholderImage:[UIImage imageNamed:@"placeholderimg"]];
    /** 查询是否收藏 */
    NSArray *allListArray = [[LYVideoInfoManager share]searchAllLocalVideoData];
    
    for (LYouVideoInfo *info in allListArray) {
        if ([info.videoId isEqualToString:model.post_id]) {
            /**此视频已收藏*/
            self.favoriteBtn.selected = YES;
            return;
        }else{
            self.favoriteBtn.selected = NO;
        }
    }
}

#pragma mark - Public Methods
- (void)startLoading {
    [self.loadingView startAnimating];
}

- (void)stopLoading {
    [self.loadingView stopAnimating];
}

- (void)showPlayBtn {
    self.playBtn.hidden = NO;
}

- (void)hidePlayBtn {
    self.playBtn.hidden = YES;
}

#pragma mark - Action
- (void)controlViewDidClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(controlViewDidClickSelf:)]) {
        [self.delegate controlViewDidClickSelf:self];
    }
}

- (void)detailBtnClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(controlViewDidClickDetailBtn:)]) {
        [self.delegate controlViewDidClickDetailBtn:self];
    }
}

- (void)favoriteBtnClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(controlViewDidClickFavorite:)]) {
        [self.delegate controlViewDidClickFavorite:self];
    }
}


#pragma mark - 懒加载

- (UIImageView *)coverImgView {
    if (!_coverImgView) {
        _coverImgView = [UIImageView new];
        _coverImgView.contentMode = UIViewContentModeScaleAspectFill;
        _coverImgView.clipsToBounds = YES;
    }
    return _coverImgView;
}

- (UIButton *)favoriteBtn {
    if (!_favoriteBtn) {
        _favoriteBtn = [UIButton new];
        [_favoriteBtn setBackgroundImage:[UIImage imageNamed:@"favorite"] forState:UIControlStateNormal];
        [_favoriteBtn setBackgroundImage:[UIImage imageNamed:@"favoriteSel"] forState:UIControlStateSelected];
        [_favoriteBtn addTarget:self action:@selector(favoriteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _favoriteBtn;
}

- (UIButton *)detailBtn {
    if (!_detailBtn) {
        _detailBtn = [UIButton new];
        [_detailBtn setBackgroundImage:[UIImage imageNamed:@"toDetail"] forState:(UIControlState)UIControlStateNormal];
        [_detailBtn setImage:[UIImage imageNamed:@"jiantou_right"] forState:UIControlStateNormal];
        [_detailBtn addTarget:self action:@selector(detailBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailBtn;
}

- (UIActivityIndicatorView *)loadingView {
    if (!_loadingView) {
        _loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        _loadingView.hidesWhenStopped = YES;
    }
    return _loadingView;
}

- (UIButton *)playBtn {
    if (!_playBtn) {
        _playBtn = [UIButton new];
        [_playBtn setImage:[UIImage imageNamed:@"ss_icon_pause"] forState:UIControlStateNormal];
        [_playBtn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _playBtn.hidden = YES;
    }
    return _playBtn;
}

-(void)playBtnClick:(UIButton *)sender{
    if (self.clickPlayButtonBlock) {
        self.clickPlayButtonBlock();
    }
}

@end
